$(function() {
	// 初始化表单验证 
	initValidateAndSubmit();
	
	// 初始化
	WASP_WIDGET.initializeSelectVal('empIdsR');
	WASP_WIDGET.initializeSelectVal('empIdsC');
	WASP_WIDGET.initializeSelectVal('publicEmailIdsC');
	WASP_WIDGET.initializeSelectVal('publicEmailIdsR');
	WASP_WIDGET.triggerSelectOnEmpId("#empIdsR", {multiple: true});
	WASP_WIDGET.triggerSelectOnEmpId("#empIdsC", {multiple: true});
	triggerSelectOnName("publicEmailIdsC");
	triggerSelectOnName("publicEmailIdsR");
	//绑定ICheck样式
	WASP_WIDGET.triggerICheck();
	
	WASP_WIDGET.triggerIRadio("input[type='radio'][name='sendType']");
	
	WASP_WIDGET.triggerIRadio("input[name=sendType]", function(radioChecked){
		radioChecked == ""?(function(){
			
		})():(function(){
			sendTypeChange();
			// 查询排序号
			$.post(PATH_PREFIX + "msgconf/getSortNoBySendType.do", {sendType: radioChecked}, function(data) {
				$("#sortNo").val(data);
			}, 'json');
		}());
		
	}, function(){});
	
	$('#backlogMenu').select2();
	
	//对select控件绑定验证事件
	triggerValidOnSelectChange('empIdsR');
});

var PATH_PREFIX = "";
function setPathPrefix(path) {
	PATH_PREFIX = path;
}

function triggerSelectOnName(publicEmailId) {
	$('#' + publicEmailId).select2({
		multiple : true,
		ajax : {
			url : PATH_PREFIX + "msgpublicemail/selectPublicEmail.do",
			type : "POST",
			data : function(params) {
				var query;
				if (params.term == null || params.term == '' || params.term == 'undefind') {
					query = {
							emailName : ""
					};
				} else {
					query = {
							emailName : params.term
					};
				}
				return query;
			},
			dataType : "json",
			processResults : function(data) {
				var results = $.map(data, function(obj) {
					return {
						id : obj.id,
						text : obj.emailName
					}
				});
				return {
					results : results
				};
			}
		},
		cache : true,
		delay : 3000,
	});
}

function initValidateAndSubmit(){
	/**
	 * 校验消息代码是否已存在
	 */
	jQuery.validator.methods["validateMsgCodeIsExist"] = (function(value, element, params){
		var flag = true;
		var msgCode = $.trim($("#msgCode").val());
		if(msgCode){
			$.ajax({
				type : "GET",
				cache : false,
				async : false,
				ifModified : true,
				dataType : "json",
				url : PATH_PREFIX + 'msgconf/validateMsgCodeIsExist.do?msgCode=' + msgCode,
				success : function(data) {
					if(false == data.success){
						flag = false;
					}
				},
				error : function(jqXHR, textStatus, errorThrown) {
					ctools.alert("校验消息代码异常！", '', 'error');
				}
			});
		}
		return flag;
	});
	
	$("#addForm").validate({
		focusCleanup: true,
		rules: {
			title: {
				required: true	  
	        },
	        sortNo: {
	        	required: true	  
	        },
	        msgCode:{
	           	required: true,
	           	validateMsgCodeIsExist: true
	        },
	        triggerDate:{
            	required: true
            },
	        sendType:{
	          	required: true
	        }
	    },
	    messages: {
	    	title: {
	    		required: '标题不能为空'	  
	    	},
	    	sortNo: {
	    		required: '排序号不能为空'	  
	    	},
	    	msgCode: {
	    		required: '消息代码不能为空',
	    		validateMsgCodeIsExist: '消息代码已经存在'
	    	},
	    	triggerDate:{
            	required: '触发条件不能为空'
            },
	        sendType:{
	        	required: '发送类型不能为空'
	        }
	    }
	});
	$("input[name='sendType']").rules("add",{required : true,messages:{required:"请选择发送类型"}});
}


//修改表单提交
function submitCheck() {
	if(!sendTypeValidate()){
		return;
	}
	
	if(!$("#publicEmailIdsR").val()){
		$("#publicEmailIdsR").append("<option value='' selected></option>");
	}
	if(!$("#publicEmailIdsC").val()){
		$("#publicEmailIdsC").append("<option value='' selected></option>");
	}

	SubmitAndPreventSecond("addForm",null);
}

//发送类型点击事件
function sendTypeChange() {
	var sendType=$('#sendTypeList').find('input[name=sendType]:checked').val();
	sendTypeShowHide(sendType);
}

//根据发送类型显示和隐藏对应的div
function sendTypeShowHide(sendType) {
	if(sendType == 'Notice'){
		$("#NoticeType").show();
		
		$("#EmailType").hide();
		$("#SmsType").hide();
		$("#BacklogType").hide();
		$("#WeChatType").hide();
	}
    if(sendType == 'Email'){
    	$("#EmailType").show();

    	$("#NoticeType").hide();
		$("#SmsType").hide();
		$("#BacklogType").hide();
		$("#WeChatType").hide();
	}
    if(sendType == 'Backlog'){
    	$("#BacklogType").show();

    	$("#EmailType").hide();
    	$("#NoticeType").hide();
		$("#SmsType").hide();
		$("#WeChatType").hide();
	}
    if(sendType == 'Sms'){
    	$("#SmsType").show();

    	$("#EmailType").hide();
    	$("#NoticeType").hide();
		$("#BacklogType").hide();
		$("#WeChatType").hide();
    }
    if(sendType == 'WeChat'){
    	$("#WeChatType").show();

    	$("#EmailType").hide();
    	$("#NoticeType").hide();
		$("#SmsType").hide();
		$("#BacklogType").hide();
    }
}	

//提交时验证发送类型对应的模块
function sendTypeValidate() {
	var sendType = $('#sendTypeList').find('input[name=sendType]:checked').val();
	
	var noticeTitleTemplate = $("#noticeTitleTemplate").val();
	var noticeContentTemplate = $("#noticeContentTemplate").val(); 
	var emailTitleTemplate = $("#emailTitleTemplate").val(); 
	var emailContentTemplate = $("#emailContentTemplate").val();
	var smsContentTemplate = $("#smsContentTemplate").val();
	var weChatContentTemplate = $("#weChatContentTemplate").val();
	var backlogTitleTemplate = $("#backlogTitleTemplate").val();
	var backlogContentTemplate = $("#backlogContentTemplate").val(); 
	var backlogMenu = $("#backlogMenu").val(); 
	var backlogUrl = $("#backlogUrl").val();
	var backlogPageUrl = $("#backlogPageUrl").val();
	var backlogParameter = $("#backlogParameter").val();
	
	var flag = true;
	var validMessage = new Array();
	
	if(sendType == 'Notice'){
		if(noticeTitleTemplate == null || noticeTitleTemplate == ''){
			flag = false;
			validMessage.push("通知标题模板不能为空");
		}
		if(noticeContentTemplate == null || noticeContentTemplate == ''){
			flag = false;
			validMessage.push("通知内容模板不能为空");
		}
	}
    if(sendType == 'Email'){
        if(emailTitleTemplate == null || emailTitleTemplate == ''){
        	flag = false;
        	validMessage.push("Email标题模板不能为空");
		}
		if(emailContentTemplate == null || emailContentTemplate == ''){
			flag = false;
			validMessage.push("Email内容模板不能为空");
		}
	}
    if(sendType == 'Sms'){
        if(smsContentTemplate == null || smsContentTemplate == ''){
        	flag = false;
        	validMessage.push("短信内容模板不能为空");
		}
	}
    if(sendType == 'Backlog'){
        if(backlogTitleTemplate == null || backlogTitleTemplate == ''){
        	flag = false;
        	validMessage.push("待办标题模板不能为空");
		}
        if(backlogContentTemplate == null || backlogContentTemplate == ''){
        	flag = false;
        	validMessage.push("待办内容模板不能为空");
        }
        if(backlogMenu == null || backlogMenu == ''){
        	flag = false;
        	validMessage.push("待办对应菜单不能为空");
        }
        if(backlogUrl == null || backlogUrl == ''){
        	flag = false;
        	validMessage.push("待办路径不能为空");
        }
        if(backlogPageUrl == null || backlogPageUrl == ''){
        	flag = false;
        	validMessage.push("待办页面路径不能为空");
        }
        if(backlogParameter == null || backlogParameter == ''){
        	flag = false;
        	validMessage.push("待办参数及值不能为空");
        }
    }
    if(sendType == 'WeChat'){
        if(weChatContentTemplate == null || weChatContentTemplate == ''){
        	flag = false;
        	validMessage.push("微信内容模板不能为空");
        }
    }
    
    if(flag == false && validMessage != null && validMessage.length > 0){
    	for (var i = 0; i < validMessage.length; i++) {
        	ctools.alert(validMessage[i], "", "warning");
    	}
    }
    return flag;
}
